"""添加客户关怀页面"""
from time import sleep

from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from page.base_page import BasePage


class AddCarePage(BasePage):
    url="/index.php?m=customer&a=caresadd"
    #定位器
    success_in_text=(By.CSS_SELECTOR,"body > div.container > div.page-header > h4")
    table_locator = (By.CSS_SELECTOR, "body > div.container > div.row > div > form > table > tbody")
    topic_name_locator = (By.CSS_SELECTOR, "#name")
    customer_name_locator = (By.NAME, "customer_name")
    select_customer_name_locator = (By.CSS_SELECTOR,
                                    "#datas > tr:nth-child(1) > td:nth-child(1) > input[type=radio]:nth-child(1)")
    btn_ok1_locator = (By.CSS_SELECTOR,
                       "body > div:nth-child(9) > div.ui-dialog-buttonpane.ui-widget-content.ui-helper-clearfix > div > button:nth-child(1) > span")
    content_locator = (By.CSS_SELECTOR,
                       "body > div.container > div.row > div > form > table > tbody > tr:nth-child(5) > td:nth-child(2) > textarea")
    contact_name_locator = (By.CSS_SELECTOR, "#contacts_name")
    select_contact_name_locator = (By.CSS_SELECTOR, "#data2 > tr:nth-child(1) > td:nth-child(1) > input[type=radio]")
    btn_ok2_locator = (By.XPATH, "/html/body/div[9]/div[3]/div/button[1]")
    time_locator = (By.CSS_SELECTOR, "#care_time")
    date_locator = (By.ID, "dpTodayInput")
    select_care_locator = (By.XPATH, "/html/body/div[5]/div[2]/div/form/table/tbody/tr[4]/td[4]/select")
    executor_locator = (By.ID, "owner_name")
    save_locator=(By.NAME,"submit")
    def success_intent(self):
        #进入添加关怀的成功判断文本
        try:
            text=self.find_element(self.success_in_text).text
        except NoSuchElementException:
            text=""
        return text

    def select_care_size(self,index):
        #选择关怀类型
        tds = self.find_element(self.table_locator).find_elements(By.TAG_NAME, "tr")[3].find_elements(By.TAG_NAME, "td")
        select = Select(tds[3].find_element(By.TAG_NAME, "select"))
        select.select_by_index(index)
    def check_first_customer(self):
        #选择第一个客户
        self.find_element(self.customer_name_locator).click()
        sleep(3)
        self.find_element(self.select_customer_name_locator).click()
        sleep(1)
        self.find_element(self.btn_ok1_locator).click()
        sleep(2)
    def input_topic(self,topic):
        #输入标题
        self.find_element(self.topic_name_locator).send_keys(topic)
    def input_content(self,content):
        #输入内容
        self.find_element(self.content_locator).send_keys(content)
    def select_first_contact(self):
        #选择第一个联系人
        self.find_element(self.contact_name_locator).click()
        sleep(3)
        self.find_element(self.select_contact_name_locator).click()
        sleep(3)
        self.find_element(self.btn_ok2_locator).click()
    def select_now_time(self):
        #选择当前时间
        self.find_element(self.time_locator).click()
        sleep(1)
        self.driver.switch_to.frame(0)
        sleep(1)
        self.find_element(self.date_locator).click()
        sleep(1)
        self.driver.switch_to.default_content()
    def save(self):
        #保存
        self.find_element(self.save_locator).click()
    def input_executor(self,executor):
        #输入执行人
        self.find_element(self.executor_locator).clear()
        self.find_element(self.executor_locator).send_keys(executor)
    def input_time(self,time):
        #输入时间
        self.find_element(self.time_locator).send_keys(time)
